package com.app.controller.student;

import com.app.pojo.*;
import com.app.service.student.StudentLoginService;
import com.app.service.student.ManagementService;
import com.app.service.teacher.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentLoginService studentLoginService;

    @Autowired
    ManagementService managementService;

    @Autowired
    TeacherService teacherService;

    /**
     * 登录
     * @param userId
     * @param password
     * @param session
     * @param model
     * @return
     */
    @PostMapping("/studentLogin")
    public String studentLogin(String userId , String password, HttpSession session, Model model) {
        Student student = studentLoginService.login(userId, password);
        if (student != null) {
            session.setAttribute("student", student);
            return "redirect:/student/index";
        } else if (student == null) {
            model.addAttribute("error", "账号或密码错误!!!");
            return "redirect:/student/login";
        }
        return "redirect:/student/login";
    }
    @GetMapping("/index")
    public String index() {
        return "/student/main";
    }

    /**
     * 学生信息查询
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/list")
    public String appList(Integer id,Model model) {
        Student student = managementService.getStudentById(id);
        Teacher teacher = teacherService.getTeacherById(student.getTeacherId());
        model.addAttribute("student", student);
        model.addAttribute("teacherName",teacher.getName());
        return "student/common/studentInfo";
    }

    /**
     * 修改信息
     * @param id
     * @param name
     * @param sex
     * @param mobilePhone
     * @param model
     * @return
     */
    @PostMapping("/update")
    public String updateInfo(Integer id,String name,Integer sex,
                            String mobilePhone,Model model){
       String error =  managementService.updateInfo(id,name,sex,mobilePhone);
       model.addAttribute("error",error);
       appList(id,model);
       return "student/common/studentInfo";
    }

    @RequestMapping(value = "toPassword")
    public String toPassword(){
        return "student/common/studentPassword";
    }

    @RequestMapping("updatePassword")
    public String updatePassword(Integer id,String oldPassword,
                                 String newPassword,Model model){
        String error = managementService.updatePassword(id,oldPassword,newPassword);
        model.addAttribute("error",error);
        if(error.equals("密码修改成功,请重新登录")){
            return "redirect:/student/login";
        }
        return "student/common/studentPassword";
    }

    @RequestMapping(value="/toGrade",method = {RequestMethod.POST,RequestMethod.GET})
    public String toGrade(Model model,String studentId){
        Grade grade = managementService.getGrade(studentId);
        model.addAttribute("grade",grade);
        return "student/common/studentGrade";
    }

    /**
     * 注销
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public String studentLogout(HttpSession session) {
        session.invalidate();
        return "redirect:/student/login";
    }
}
